var QcReportInfo = function (reportList) {
  BaseModel.call(this);

  var self = this;
  var fn = {
    initData: function (reportList) {
      reportList = _.orderBy(reportList, [function(report){
        var createTime = report.createTime;
        if (createTime===null) {
          return -Infinity;
        }
        return moment(createTime).valueOf();
      }],['desc']);
      self.reportRes = reportList;
      this.formatReportData(reportList);
      self.rowIndex = 0;
    },

    formatReportData: function (reportList) {
      var reportGroup = _.groupBy(reportList, function (report) {
        return `${report.orderId}_${report.processId}`;
      });

      var reportTableList = [];
      var fields = ['companyOrderId', 'factoryId', 'customerId', 'processName', 'workShopName'];
      for (var key in reportGroup) {
        var reports = reportGroup[key];
        var type1CheckList = [],
          type2CheckList = [],
          type3CheckList = [],
          type4CheckList = [];
        for (var i = 0; i < reports.length; i++) {
          var report = reports[i];
          var type = report.type;
          var intervalTime = report.intervalTime;
          var typeObj;
          if ([1, 2, 3].indexOf(type) !== -1) {
            typeObj = {
              createUser: report.createUser,
              createTime: report.createTime,
              intervalTime: intervalTime,
              details: report.detail
            };
          } else {
            typeObj = {
              createUser: report.createUser,
              qualifiedCount: report.qualifiedCount,
              noQualifiedCount: report.noQualifiedCount
            };
          }
          if (type === 1) {
            type1CheckList.push(typeObj);
          } else if (type === 2) {
            var selfCheckTime = report.selfCheckTime * 60;
            var vacancyCount = Math.floor(_.round(intervalTime / selfCheckTime, 4));
            typeObj.vacancyCount = vacancyCount;
            type2CheckList.push(typeObj);
          } else if (type === 3) {
            var siteCheckTime = report.siteCheckTime * 60;
            var vacancyCount = Math.floor(_.round(intervalTime / siteCheckTime, 4));
            typeObj.vacancyCount = vacancyCount;
            type3CheckList.push(typeObj);
          } else if (type === 4) {
            type4CheckList.push(typeObj);
          }
        }

        this.calAverageTime(type2CheckList);
        this.calAverageTime(type3CheckList);

        var typeCheckList = [];
        var maxCount = _.max([type1CheckList.length, type2CheckList.length, type3CheckList.length, type4CheckList.length]);
        for (var i = 0; i < maxCount; i++) {
          var obj = {};
          var type1Check = type1CheckList[i];
          if (type1Check === undefined) {
            type1Check = null;
          }
          obj.type1Check = type1Check;

          var type2Check = type2CheckList[i];
          if (type2Check === undefined) {
            type2Check = null;
          }
          obj.type2Check = type2Check;

          var type3Check = type3CheckList[i];
          if (type3Check === undefined) {
            type3Check = null;
          }
          obj.type3Check = type3Check;

          var type4Check = type4CheckList[i];
          if (type4Check === undefined) {
            type4Check = null;
          }
          obj.type4Check = type4Check;

          typeCheckList.push(obj);
        }

        var report = reports[0];
        var reportObj = {
          id: `${report.orderId}_${report.processId}_${i}`,
          orderId: report.orderId,
          processId: report.processId,
          workShopId: report.workShopId,
          typeCheckList: typeCheckList
        };
        for (var i = 0; i < fields.length; i++) {
          var field = fields[i];
          var value = report[field];
          if (typeof value === 'string') {
            value = value.trim();
          }
          if (SysUtil.isEmpty(value)) {
            value = '';
          }
          reportObj[field] = value;
        }
        reportTableList.push(reportObj);
      }

      self.reportList = reportTableList;
    },

    calAverageTime: function (checkList) {
      var checkGroup = _.groupBy(checkList, 'createUser');
      for (var key in checkGroup) {
        var list = checkGroup[key];
        var len = list.length;
        var sumIntervalTime = _.sumBy(list, 'intervalTime');
        var avgTime = _.round(sumIntervalTime / len, 2);
        for (var i = 0; i < len; i++) {
          list[i].avgTime = avgTime;
        }
      }
    }
  };

  this.addRowIndex = function () {
    var rowIndex = this.rowIndex;
    var len = this.reportList.length;
    rowIndex = (rowIndex + 1) % len;
    this.rowIndex = rowIndex;
  };

  this.resetData = function (reportList) {
    reportList = _.orderBy(reportList, [function(report){
      var createTime = report.createTime;
      if (createTime===null) {
        return -Infinity;
      }
      return moment(createTime).valueOf();
    }],['desc']);
    this.reportRes = reportList;
    fn.formatReportData(reportList);
  };

  this.resetRowIndex = function () {
    var rowIndex = this.rowIndex;
    var len = this.reportList.length;
    if (rowIndex >= len) {
      this.rowIndex = 0;
    }
  };

  this.getReportData = function () {
    var rowIndex = this.rowIndex;
    var reportList = this.reportList;

    if (rowIndex === 0) {
      return reportList;
    }

    var reports = reportList.slice(rowIndex);
    var reportArray = reportList.slice(0, rowIndex);
    reports.pushArray(reportArray);
    return reports;
  };

  fn.initData(reportList);
};
